var util = require('../../utils/util.js');
var api = require('../../utils/api.js');

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    number:'',
    question:'',
    question_types:'',
    pic: '',
    items: [
      { name: 'A', value: '' },
      { name: 'B', value: '' },
      { name: 'C', value: '' },
      { name: 'D', value: '' }
    ],
    answer: '',
    chapter: '',
    explain: '',
    result:0,
    display:'',
    type:''
  },
  onLoad: function () {
    var type = wx.getStorageSync('type');
    this.data.type = type;
    var question_list_1 = wx.getStorageSync(type + '_question_list_1');
    if (!question_list_1) {
      wx.showLoading({
        mask: true,
        title: '出题中，请稍后',
      })
      this.requestQuestion(type);
    } else {
      var number = wx.getStorageSync(type + '_number_1');
      number = number ? number : 0;
      this.getQuestion(number);
    }
  },
  
  requestQuestion:function(type){
    if (!wx.getStorageSync(type + '_question_list_1')){
      api.get({
        url: 'Login/getQuestionList',
        data: {
           type    : type,
           subject :1
        },
        success: res => {
          if (res.question_list){
           
            wx.setStorageSync(type+'_question_list_1', res.question_list);
            var number = wx.getStorageSync(type + '_number_1');
            number = number ? number : 0;
            this.getQuestion(number);
            wx.hideLoading();
          }
        },
      });
    }
  },

  radioChange:function(e){
    var v = e.detail.value;
    this.setData({ result: (v == this.data.answer) ? 1 : 0 });
    this.setData({ display: 1 })
  },

  upQuestion:function(){
     this.setData({result:0,display:0})
     if(this.data.number<2) return;
     this.getQuestion(this.data.number-2);
  },
  downQuestion:function(){
     this.setData({ result: 0, display: 0 })

     this.getQuestion(this.data.number);
  },
  goIndex:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  getQuestion:function(number){
     var type = wx.getStorageSync('type');
     var question_info = wx.getStorageSync(type + '_question_list_1')[number];
     var question_types = question_info.question_types;
  
     this.setData({
       number: number+1,
       question: question_info.question,
       question_types: question_info.question_types,
       answer: question_info.answer,
       chapter: question_info.chapter,
       explain: question_info.explain,
       pic: question_info.pic,
      })

     if (question_types == '1'){
         this.setData({
          items: [
            { name: 'A', value: question_info.option1 },
            { name: 'B', value: question_info.option2 },
            { name: 'C', value: question_info.option3 },
            { name: 'D', value: question_info.option4 }
          ]})
       }
      if (question_types == '2') {
        this.setData({
          items: [
            { name: '对', value: '正确' },
            { name: '错', value: '错误' },
          ]
        })
      }
      wx.setStorageSync(type+'_number_1', number);
  }
  
})
